package pzl.com.library.data;

import java.util.ArrayList;
import java.util.HashMap;

import pzl.com.library.chart.BaseChart;

/**
 * @author zl.peng
 * @version [1.0, 2016-11-21]
 */
public abstract class BaseData<E extends Entry> {
    private static final String TAG = "BaseData";

    private HashMap<String, DataList> allData = new HashMap<String, DataList>();

    protected BaseChart mBaseChart;

    public BaseData(BaseChart baseChart) {
        mBaseChart = baseChart;
    }

    final public void addDataList(String key, DataList dataList) {
        allData.put(key, dataList);
        //更新longest size
        onDataUpdate();
    }

    /**
     * 某个数据集合的长度
     *
     * @param key
     * @return
     */
    public int getDataListSize(String key) {
        if (!allData.containsKey(key)) return -1;
        return allData.get(key).size();
    }

    /**
     * @param key
     * @param index
     * @return
     */
    public Entry getEntry(String key, int index) {
        if (!allData.containsKey(key)) return null;
        return allData.get(key).getEntry(index);
    }

    /**
     * @param key
     * @param entry
     */
    public void addEntry(String key, Entry entry) {
        if (!allData.containsKey(key)) addDataList(key, new DataList());
        allData.get(key).add(entry);
        //更新longest size
        onDataUpdate();
    }

    public void addAll(String key, ArrayList subDataList) {
        if (!allData.containsKey(key)) addDataList(key, new DataList());
        allData.get(key).addAll(subDataList);
        //更新longest size
        onDataUpdate();
    }

    public HashMap<String, DataList> getAllData() {
        return allData;
    }

    /**
     * 数据集合的条数
     *
     * @return
     */
    public int dataListNumber() {
        return allData.size();
    }

    protected abstract void onDataUpdate();

}
